/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Niudan;
import com.simon.mengkou.data.bean.base.NiudanSKU;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.view.RadioButton;
import com.simon.mengkou.ui.view.StrokeTextView;
import com.simon.mengkou.utils.UtilOuer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/19.
 * desc    :
 */
public class ImagesExpandableAdapter extends BaseExpandableListAdapter{
    private Context mContext;
    private List<Niudan> mDatas;
    private LayoutInflater mInflater;

    public ImagesExpandableAdapter(Context context) {
        super();
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = new ArrayList<>();


    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getGroupCount() {
        return UtilList.getCount(mDatas);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(UtilList.isNotEmpty(mDatas)) {
            return UtilList.getCount(mDatas.get(groupPosition).getSku());
        } else {
            return 0;
        }
    }

    @Override
    public Niudan getGroup(int groupPosition) {
        return mDatas.get(groupPosition);
    }

    @Override
    public View getGroupView(int groupPosition, final boolean isExpanded,
                             View convertView, ViewGroup parent) {
        final GroupHolder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_image_group_item, null);
            holder = new GroupHolder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }


        Niudan data = getGroup(groupPosition);

        if(isExpanded && UtilList.isNotEmpty(data.getSku())) {//展开
            holder.mIvBottom.setVisibility(View.GONE);
            holder.mIvComplete.setVisibility(View.GONE);
            holder.mViewBg.setVisibility(View.VISIBLE);
            holder.mIvState.setBackgroundResource(R.drawable.image_ic_close);
        } else {//折叠
            holder.mIvBottom.setVisibility(View.VISIBLE);
            holder.mIvComplete.setVisibility(View.VISIBLE);
            holder.mViewBg.setVisibility(View.GONE);
            holder.mIvState.setBackgroundResource(R.drawable.image_ic_open);
        }

        //扭蛋图片
        OuerApplication.mImageLoader.loadCircleImage(holder.mSdvImage, data.getImage().getUrl());
        //扭蛋名
        holder.mStvName.setText(data.getName());

        return convertView;
    }

    @Override
    public NiudanSKU getChild(int groupPosition, int childPosition) {
        return mDatas.get(groupPosition).getSku().get(childPosition);
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        final ChildHolder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_image_child_item, null);
            holder = new ChildHolder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }

        final NiudanSKU data = getChild(groupPosition, childPosition);

        if(childPosition + 1 == getChildrenCount(groupPosition)) {
            holder.mLlSendRoot.setVisibility(View.VISIBLE);
            //邮寄
            holder.mSdvSend.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    ArrayList<Niudan> niudans = new ArrayList<Niudan>();
                    for(Niudan niudan : mDatas) {
                        List<NiudanSKU> skus = niudan.getSku();
                        if(UtilList.isNotEmpty(skus)) {
                            boolean flag = false;
                            for(NiudanSKU sku : skus) {
                                if(sku.isCheck()) {
                                    flag = true;
                                    break;
                                }
                            }

                            if(flag) {
                                niudans.add(niudan);
                            }
                        }
                    }

                    if(UtilList.isNotEmpty(niudans)) {
                        OuerDispatcher.presentOrderActivity(mContext, niudans);
                    } else {
                        UtilOuer.toast(mContext, R.string.image_send_empty);
                    }
                }
            });
        } else {
            holder.mLlSendRoot.setVisibility(View.GONE);
        }

        //sku图片
        if(data.getCount() == 0 && !data.isSend()) {
            holder.mIvBorder.setBackgroundResource(R.drawable.image_ic_sku_disable);
        } else {
            holder.mIvBorder.setBackgroundResource(R.drawable.image_ic_sku_normal);
            OuerApplication.mImageLoader.loadCircleImage(holder.mSdvImage,
                    data.getImage().getUrl());
        }

        //名字
        holder.mTvName.setText(data.getName());

        //
        if(data.getCount() == 0 ) {
            holder.mRbRadio.setVisibility(View.INVISIBLE);
        } else {
            holder.mRbRadio.setChecked(data.isCheck());
            holder.mRbRadio.setVisibility(View.VISIBLE);
            holder.mRbRadio.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    data.setCheck(!data.isCheck());
                    holder.mRbRadio.setChecked(data.isCheck());
                }
            });
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition,  int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }


    public void addList(List<Niudan> items) {
        if (UtilList.isNotEmpty(items)) {
            mDatas.addAll(items);
            notifyDataSetChanged();
        }
    }

    public void clearAll() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    class GroupHolder {
        @Bind(R.id.image_group_id_bottom) ImageView mIvBottom;
        @Bind(R.id.image_group_id_complete) ImageView mIvComplete;
        @Bind(R.id.image_group_id_bg) View mViewBg;
        @Bind(R.id.image_group_id_image) SimpleDraweeView mSdvImage;
        @Bind(R.id.image_group_id_name)
        StrokeTextView mStvName;
        @Bind(R.id.image_group_id_state) ImageView mIvState;
    }

    class ChildHolder {
        @Bind(R.id.image_child_id_send_root) LinearLayout mLlSendRoot;
        @Bind(R.id.image_child_id_send) SimpleDraweeView mSdvSend;
        @Bind(R.id.image_child_id_border) ImageView mIvBorder;
        @Bind(R.id.image_child_id_image) SimpleDraweeView mSdvImage;
        @Bind(R.id.image_child_id_name) TextView mTvName;
        @Bind(R.id.image_child_id_radio)
        RadioButton mRbRadio;
    }
}
